package com.hsmpay.mobile.action.account.qian48;

import com.hsmpay.common.pojo.qian4.Qian4SendLog;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.URLDecoderTools;
import com.hsmpay.mobile.action.account.fc.PurchaseBaseAction;
import com.hsmpay.mobile.pojo.terminal.TerminalPrivileges;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import com.hsmpay.mobile.pojo.trans.TransPriorSettle;
import com.hsmpay.mobile.pojo.user.UserTransLimit;
import com.hsmpay.mobile.service.user.UserTransLimitService;
import com.hsmpay.mobile.service.zytl.ZYTLParamService;
import com.hsmpay.mobile.util.icc.ME30DataDecode;
import com.hsmpay.qian4.service.ds.impl.ZYTLPayServiceImpl;
import com.test.JDPushConfig;
import com.test.JdPush;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.hsmpay.qian4.pojo.FeeResult;
import com.hsmpay.qian4.pojo.PurchaseResult;
import com.hsmpay.utils.TimeUtils;

import javax.annotation.Resource;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 千48 转账 action
 * @author 颜铃璋
 * @date 2013-8-27
 */
@Controller("q4TransferAction")
@Scope("prototype")
public class Q4TransferAction extends Q4BaseAction {
    private static final long serialVersionUID = -6685556721396542845L;
    private static Logger log = Logger.getLogger(Q4TransferAction.class);

    public static final String TRANSFER_FUNCKEY = "CARD2CARDTRANSFER";

    @Resource(name = "userTransLimitService")
    private UserTransLimitService<UserTransLimit, Long> userTransLimitService;

    /**
     * 转账
     * @return
     * @throws Exception
     */
    public String transfer() throws Exception {
        log.debug("--- transfer() start ---");
        clientType = Integer.parseInt(getJsonString("clientType"));
        terminalTypeId = Long.parseLong(getJsonString("terminalTypeId"));
        roleId = Long.parseLong(getJsonString("roleId"));

        userId = Long.parseLong(getJsonString("userId"));
        functionKey = getJsonString("functionKey");
        merchantOrderId = getJsonString("merchantOrderId");
        merchantOrderTime = getJsonString("merchantOrderTime");
        terminalId = getJsonString("terminalId");
        transAmt = getJsonString("transAmt");
        accountNumber = getJsonString("accountNumber");
        pin = getJsonString("pin");
        track2Data = getJsonString("track2Data");
        track3Data = getJsonString("track3Data");
        accountNumber2 = getJsonString("accountNumber2");
        accountName = getJsonString("accountName");
        cardholderSign = getJsonString("cardholderSign");
        dynamicKeyData = getJsonString("dynamicKeyData");
        merchantSign = getJsonString("merchantSign");
        senderSign = getJsonString("senderSign");
        serviceInfo = getJsonString("serviceInfo");
        terminalInfo = getJsonString("terminalInfo");
        mac = getJsonString("mac");
        encryType = getJsonString("encryType");
        icCardYu = getJsonString("icCardYu");
        Integer isNew = StringUtils.isEmpty(getJsonString("isNew")) ? 0 : Integer.parseInt(getJsonString("isNew"));
        String isNeedNameId = getJsonString("isNeedNameId");
        String idCard = getJsonString("idCard");
        String purchaseName = getJsonString("purchaseName");
        log.debug("userId = " + userId);//用户ID
        log.debug("functionKey = " + functionKey);//用户ID
        log.debug("clientType = " + clientType);//
        log.debug("terminalTypeId = " + terminalTypeId);//

        log.debug("merchantOrderId = " + merchantOrderId);//商户订单
        log.debug("merchantOrderTime = " + merchantOrderTime);//商户订单时间
        log.debug("terminalId = " + terminalId);//终端代码
        //log.debug("transId = " + transId);//交易流水号
        log.debug("transAmt = " + transAmt);//交易金额
        log.debug("accountNumber = " + accountNumber);//主账户
        log.debug("pin = " + pin);//个人身份标识码
        log.debug("track2Data = " + track2Data);//磁道2数据
        log.debug("track3Data = " + track3Data);//磁道3数据
        log.debug("accountNumber2 = " + accountNumber2);//转入卡卡号
        log.debug("accountName = " + accountName);//转入账户名
        log.debug("dynamicKeyData = " + dynamicKeyData);//动态密钥数据

        log.debug("senderSign = " + senderSign);//发送方数字签名
        log.debug("serviceInfo = " + serviceInfo);//服务信息
        log.debug("terminalInfo = " + terminalInfo);//终端信息 手机串号、号码、os、序列号等
        log.debug("mac = " + mac);

        if (null == userId) {
            log.warn("转账时, 传入参数userId为空");
            sendAppErrorMsg("6001", "userId");
            return null;
        }
        if (StringUtils.isBlank(functionKey)) {
            log.warn("转账时, 传入参数functionKey为空");
            sendAppErrorMsg("6001", "functionKey");
            return null;
        }
        if (StringUtils.isBlank(merchantOrderId)) {
            log.warn("转账时, 传入参数merchantOrderId为空");
            sendAppErrorMsg("6001", "merchantOrderId");
            return null;
        }
        if (null == merchantOrderTime) {
            log.warn("转账时, 传入参数merchantOrderTime为空");
            sendAppErrorMsg("6001", "merchantOrderTime");
            return null;
        }
        if (null == transAmt) {
            log.warn("转账时, 传入参数transAmt为空");
            sendAppErrorMsg("6001", "transAmt");
            return null;
        }
        if (StringUtils.isBlank(accountNumber)) {
            log.warn("转账时, 传入参数accountNumber为空");
            sendAppErrorMsg("6001", "accountNumber");
            return null;
        }
        if (StringUtils.isBlank(pin)) {
            log.warn("转账时, 传入参数pin为空");
            sendAppErrorMsg("6001", "pin");
            return null;
        }
        if (StringUtils.isBlank(track2Data)) {
            log.warn("转账时, 传入参数track2Data为空");
            sendAppErrorMsg("6001", "track2Data");
            return null;
        }
        /*if (StringUtils.isBlank(track3Data)) {
			log.warn("转账时, 传入参数track3Data为空");
			sendAppErrorMsg("6001", "track3Data");
			return null;
		}*/
        if (StringUtils.isBlank(accountNumber2)) {
            log.warn("转账时, 传入参数accountNumber2为空");
            sendAppErrorMsg("6001", "accountNumber2");
            return null;
        }
        if (StringUtils.isBlank(accountName)) {
            log.warn("转账时, 传入参数accountName为空");
            sendAppErrorMsg("6001", "accountName");
            return null;
        }
        if (1 == isNew) {
            if (StringUtils.isBlank(isNeedNameId)) {
                log.warn("转账时, 传入参数isNeedNameId为空");
                sendAppErrorMsg("6001", "isNeedNameId");
                return null;
            }
            if ("1".equals(isNeedNameId)) {
                if (StringUtils.isBlank(idCard)) {
                    log.warn("转账时, 传入参数idCard为空");
                    sendAppErrorMsg("6001", "idCard");
                    return null;
                }
                if (StringUtils.isBlank(purchaseName)) {
                    log.warn("转账时, 传入参数userName为空");
                    sendAppErrorMsg("6001", "userName");
                    return null;
                }
            }
        }

        //单笔限额
        double limitMoney = Double.parseDouble(transAmt) / 100;
        if (limitMoney > 20000 && userId.intValue() != 2990) {//金额超限
            sendAppErrorMsg("6071", "20000");
            return null;
        }

        Map<String, Object> result = null;
        User user = null;
        Qian4SendLog send = null;
        TransOrder transOrder = null;
        TransPriorSettle transPriorSettle = null;
        try {
            functionKey = TRANSFER_FUNCKEY;
            if (!checkPIV(functionKey) || (null == isNew || 1 != isNew)) {
                log.warn("转账时, 银行接口暂停使用！");
                sendAppErrorMsg("6002");
                return null;
            }

            double transMoney = Double.parseDouble(transAmt) / 100;
            Map<String, Object> map = userTransLimitService.transCheck(userId, roleId, accountNumber, transMoney, terminalTypeId, "CARD2CARDTRANSFER", null);
            if (map != null) {
                sendAppErrorMsg(map.get("code").toString(), map.get("value").toString());
                return null;
            }

            //屏蔽伪卡
            if (transRefuseCardService.isRefuseCard(accountNumber)) {
                sendAppErrorMsg("6073", accountNumber);
                return null;
            }

            if ("1".equals(encryType)) {
                track2Data = ME30DataDecode.decryptTrace2(track2Data);
                //icCardYu.replace(oldChar, newChar)
            } else if ("2".equals(encryType)) {
                String trackOData = getJsonString("trackOData");
                String[] trackA = ME30DataDecode.decryptTrace2_aic(trackOData, track2Data);
                track2Data = trackA[0];
                track3Data = trackA[1];
            }
            track2Data = track2Data.replaceAll("F", "");//屏蔽二磁道有F的情况

            result = new HashMap<String, Object>();

            accountName = URLDecoderTools.decodeUTF8(accountName);
            user = userService.getUserById(userId);
            if (null != user) {
//				if(userId.intValue() != 2990){//限制笔数
//					boolean flag = userTransLimitService.transferNumberLimit(userId);
//					if(flag){
//						log.info("用户：userId="+userId+" 卡卡转账次数达到上限!");
//						sendAppErrorMsg("6060",userId+"");
//						return null;
//					}
//				}
                User user1=new User();
                user1.setId(userId);
                user1.setDeleted(0);
                user= userService.searchEntity(user1);
                if(user==null){
                    Map<String, Object> resMap = new HashMap<String, Object>();
                    log.warn("卡卡转账时, 用户Id=" + userId + " 对应的用户不存在!");
                    resMap.put("reqCode", "6004");
                    resMap.put("reqMsg", "用户："+userId+ " 该用户被销户或暂停使用！");
                    resMap.put("channelId", "");
                    sendAppMsg(resMap);
                    return null;
                }
                //判断日期是否是今天
                Date date = new Date();
                String today = DateConvertUtil.dateString(date, "yyyyMMdd");//
                if (!merchantOrderTime.startsWith(today)) {
                    merchantOrderId = getMerchantOrderId(date, user.getLoginName());
                    merchantOrderTime = DateConvertUtil.dateString(date, "yyyyMMddHHmmss");
                }

                //查出商户订单号 是否存在
                transOrder = transOrderService.searchOrderByMerchantOrderId(user.getLoginName(), merchantOrderId);
                if (null != transOrder) {
                    sendAppErrorMsg("6101", merchantOrderId);
                    return null;
                }

                TerminalPrivileges tp = terminalPrivilegesService.searchPrivilegesByFunctionKey(TRANSFER_FUNCKEY);
                Integer channelCode = tp.getTchannel().intValue();

                if (null == channelCode) {
                    channelCode = PurchaseBaseAction.TC_ZHONGYI;
                }

                send = new Qian4SendLog();
                send.setCallType(0);
                send.setUserId(userId);
                send.setApplication("Transfer.Req");
                send.setOrderTypeId(CARD_TRANSFER);
                send.setTransType("100003");//
                send.setSendTime(date);
                send.setSendSeqId(TimeUtils.getTimeToString());
//				send.setMerchantId(merchantId);//商户代码 ##
                send.setMerchantOrderTime(DateConvertUtil.stringDate(merchantOrderTime, "yyyyMMddHHmmss"));
                send.setTerminalId(terminalId);
                send.setPlatformId(platformId);
                send.setRoleId(roleId);

                send.setMerchantOrderId(merchantOrderId);//## 商户订单号
                send.setTransTime(date);
                send.setCurrency("156");
                send.setTransAmt(transAmt);

                send.setAccountNumber(accountNumber);
                send.setPin(pin);
                send.setTrack2Data(track2Data);
                send.setTrack3Data(track3Data);
                send.setAccountNumber2(accountNumber2);//信用卡卡号
                send.setAccountName(accountName);//信用卡卡号
                send.setDynamicKeyData(dynamicKeyData);

                send.setSenderSign(senderSign);
                send.setServiceInfo(serviceInfo);
                send.setTerminalInfo(terminalInfo);
                send.setMac(mac);
                send.setCollectionType(channelCode);
                send.setIp(URLDecoderTools.getRemoteHost(getRequest()));//IP
                send.setIcCardYu(icCardYu);
                send.setCardTypeN(StringUtils.isEmpty(icCardYu) ? 1 : 2);//1 磁条卡 2 IC卡
                send.setIsNeedNameId(("1".equals(isNeedNameId) ? 1 : 0));
                send.setIdCard(idCard);
                send.setPurchaseName(purchaseName);
                send.setIsNew(isNew);

                //查询手续费
                double money = Double.parseDouble(transAmt) / 100;
                double fee = 0.0;
                switch (channelCode) {
                    case PurchaseBaseAction.TC_ZHONGYI:
                        FeeResult fr = getZYFee(accountNumber2, transAmt,roleId);
                        if (!"0000".equals(fr.getReqCode())) {
                            sendAppErrorMsg("6062", fr.getReqMsg());
                            return null;
                        }
                        fee = Double.valueOf(fr.getFee());
                        break;
                }
                send.setFee((int) (fee * 100) + "");

                transOrder = new TransOrder();
                transOrder.setOrderNum(getOrderNumByDate(date));
                transOrder.setOrderTypeId(CARD_TRANSFER);//卡卡转账
                transOrder.setUserId(userId);
                transOrder.setPUserId(user.getPUserId());//父用户Id
                transOrder.setTransMoney(money);
                transOrder.setFeeMoney(fee);//手续费
                transOrder.setChargeAmount(money + transOrder.getFeeMoney());
                transOrder.setRootCategory("账务管理");
                transOrder.setCategory("卡卡转账");
                //transOrder.setName(userName);//收款方
                transOrder.setBankCard(accountNumber);
                //transOrder.setOpeningBank(openingBank);
                transOrder.setObjectiveName(accountName);//对方持卡人姓名
                transOrder.setObjectiveCard(accountNumber2);//对方银行卡
                //transOrder.setObjectiveOpBank(user.getOpeningBank());//对方银行
                transOrder.setStatus(3);//交易状态  0交易失败 1交易成功 2银行交易处理中 3 未付款 4等待对方付款5关闭交易
                transOrder.setClearing(0);//结算状态 0未结算 1结算
                //transOrder.setDescription(description);//交易详情
                transOrder.setCreateDate(date);
                transOrder.setUpdateLongTime(date.getTime());

                transOrder.setBrokerage(0.0000);
                transOrder.setDealerBrokerage(0.0000);
                transOrder.setTerminalTypeId(terminalTypeId);
                transOrder.setRoleId(roleId);
                transOrder.setFeeRate(0.00);//用户费率
                transOrder.setFdMaxFee(0.00);//用户封顶最高
                transOrder.setFdMinFee(0.00);//用户封顶最低
                transOrder.setCardTypeN(send.getCardTypeN());//1 磁条卡 2 IC卡
                transOrder.setName(purchaseName);//出帐姓名
                transOrder.setIdCard(idCard);//出帐身份证
                transOrder.setClientType(clientType);//客户端类型

                //下单 并保存发送数据
                transPriorSettle = accountManageQ4Service.advanceOrder(transOrder, send, 1);
                PurchaseResult pr = null;

                switch (channelCode) {
                    case PurchaseBaseAction.TC_ZHONGYI:
                        pr = ds_zytlPayService.transfer(send);
                        break;
                }
                if (0 == pr.getStatus()) {//错误
                    log.debug("卡卡转账失败");
                    result.put("bankResp", "0");
                    result.put("reqCode", pr.getReqCode());
                    result.put("reqMsg", pr.getReqMsg());
                    accountManageQ4Service.receivablesError(transPriorSettle, pr.getReqCode(), pr.getReqMsg());
                    qian4SendLogService.updateResCodeMassage(transPriorSettle.getSendId(), pr.getReqCode(), pr.getReqMsg());
//                    User u =  new User();
//                    u.setId(userId);
//                    //查询数据库中该用户的极光信息
//                    u = userService.searchEntity(u);
//                    //苹果手机
//                    if(clientType==3){
//                        String Status="";
//                        if(transPriorSettle.getStatus()==1){
//                            Status="交易成功！";
//                        }else if(transPriorSettle.getStatus()==2){
//                            Status="银联处理中!";
//                        }else if(transPriorSettle.getStatus()==3){
//                            Status="交易失败!";
//                        }
//                        //苹果
//                        String appKey= JDPushConfig.getAttribute("IOS_JDPush_appKey");
//                        String masterSecret=JDPushConfig.getAttribute("IOS_JDPush_masterSecret");
//                        Date datetime=new Date();
//                        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String time=format.format(datetime);
//                        //调用方法进行推送消息
//                        JdPush.ALERT=userId+"您好！您的中国"+transPriorSettle.getAcquirersName()+
//                                "账户\n转出卡号："+accountNumber+ "\n与订单时间"+
//                                time+
//                                "\n进行一笔卡卡转账操作"+ "\n转入卡号："+accountNumber2+"\n转入金额"+(Double.parseDouble(transAmt) / 100.0)+
//                                "\n交易状态："+Status+"\n终端编号："+transPriorSettle.getTerminalId();
//                        JdPush.MSG_CONTENT="交易信息提醒！";
//                        JdPush.TITLE="花生米移动支付";
//                        JdPush.REGISTRATION_ID=u.getRegistration_id();
//                        JdPush.testSendPush(appKey, masterSecret);
//                        //安卓手机
//                    }else if(clientType==2){
//                        String Status="";
//                        if(transPriorSettle.getStatus()==1){
//                            Status="交易成功！";
//                        }else if(transPriorSettle.getStatus()==2){
//                            Status="银联处理中!";
//                        }else if(transPriorSettle.getStatus()==3){
//                            Status="交易失败!";
//                        }
//                        //安卓
//                        String appKey= JDPushConfig.getAttribute("Android_JDPush_appKey");
//                        String masterSecret=JDPushConfig.getAttribute("Android_JDPush_masterSecret");
//                        Date datetime=new Date();
//                        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String time=format.format(datetime);
//                        //调用方法进行推送消息
//                        JdPush.ALERT=userId+"您好！您的中国"+transPriorSettle.getAcquirersName()+
//                                "账户\n转出卡号："+accountNumber+
//                                "\n与订单时间"+
//                                time+
//                                "\n进行一笔卡卡转账操作"+ "\n转入卡号："+accountNumber2+"\n转入金额"+(Double.parseDouble(transAmt) / 100.0)+
//                                "\n交易状态："+Status+"\n终端编号："+transPriorSettle.getTerminalId();
//                        JdPush.MSG_CONTENT="交易信息提醒！";
//                        JdPush.TITLE="花生米移动支付";
//                        JdPush.REGISTRATION_ID=u.getRegistration_id();
//                        JdPush.testSendPush(appKey, masterSecret);
//                    }
                } else {
                    transPriorSettle = accountManageQ4Service.purchaseResolveResult(transPriorSettle, send, pr, null, user.getOrganizationId());
                    result.put("reqCode", transPriorSettle.getResponseCode());
                    if (StringUtils.isNotBlank(transPriorSettle.getBankMessage()))
                        result.put("reqMsg", transPriorSettle.getBankMessage());

                    qian4SendLogService.updateResCodeMassage(send.getId(), transPriorSettle.getResponseCode(), transPriorSettle.getBankMessage());

                    //用接口调用  需要返回
                    //result.put("transId",transPriorSettle.getBankOrderNum());
                    result.put("orderNum", transOrder.getOrderNum());
                    result.put("transTime", DateConvertUtil.dateString(transPriorSettle.getCreateDate(), "HH:mm:ss"));//交易时间
                    result.put("accountNumber", send.getAccountNumber());//
                    result.put("accountNumber2", send.getAccountNumber2());
                    result.put("batchId", transPriorSettle.getBatchId());//批次号
                    result.put("searchId", transPriorSettle.getSearchId());//查询号
                    result.put("authNum", transPriorSettle.getAuthorizationCode());//授权号
                    result.put("refId", transPriorSettle.getBankRef());//参考和
                    result.put("issueBank", transPriorSettle.getIssueBank());//发卡行名称
                    result.put("cardType", transPriorSettle.getCardType());//卡类别
                    result.put("expireDate", transPriorSettle.getExpireDate());//有效期
                }
                sendAppMsg(result);
//                //推送消息
//                User u =  new User();
//                u.setId(userId);
//                //查询数据库中该用户的极光信息
//                u = userService.searchEntity(u);
//                //苹果手机
//                if(clientType==3){
//                    String Status="";
//                    if(transPriorSettle.getStatus()==1){
//                        Status="交易成功！";
//                    }else if(transPriorSettle.getStatus()==2){
//                        Status="银联处理中!";
//                    }else if(transPriorSettle.getStatus()==3){
//                        Status="交易失败!";
//                    }
//                    //苹果
//                    String appKey= JDPushConfig.getAttribute("IOS_JDPush_appKey");
//                    String masterSecret=JDPushConfig.getAttribute("IOS_JDPush_masterSecret");
//                    Date datetime=new Date();
//                    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String time=format.format(datetime);
//                    //调用方法进行推送消息
//                    JdPush.ALERT=userId+"您好！您的中国"+transPriorSettle.getAcquirersName()+
//                            "账户\n卡号："+transOrder.getBankCard()+"\n与订单时间"+
//                            time+
//                            "\n进行一笔卡卡转账操作"+
//                            "\n转入卡号："+accountNumber2+"\n转入金额"+(Double.parseDouble(transAmt) / 100.0)+
//                            "\n交易状态："+Status+
//                            "\n终端编号："+transPriorSettle.getTerminalId();
//                    JdPush.MSG_CONTENT="交易信息提醒！";
//                    JdPush.TITLE="花生米移动支付";
//                    JdPush.REGISTRATION_ID=u.getRegistration_id();
//                    JdPush.testSendPush(appKey, masterSecret);
//                    //安卓手机
//                }else if(clientType==2){
//                    String Status="";
//                    if(transPriorSettle.getStatus()==1){
//                        Status="交易成功！";
//                    }else if(transPriorSettle.getStatus()==2){
//                        Status="银联处理中!";
//                    }else if(transPriorSettle.getStatus()==3){
//                        Status="交易失败!";
//                    }
//                    //安卓
//                    String appKey= JDPushConfig.getAttribute("Android_JDPush_appKey");
//                    String masterSecret=JDPushConfig.getAttribute("Android_JDPush_masterSecret");
//                    Date datetime=new Date();
//                    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String time=format.format(datetime);
//                    //调用方法进行推送消息
//                    JdPush.ALERT=userId+"您好！您的中国"+transPriorSettle.getAcquirersName()+
//                            "账户\n卡号："+transOrder.getBankCard()+ "\n与订单时间"+
//                            time+
//                            "\n进行一笔卡卡转账操作"+
//                            "\n转入卡号："+accountNumber2+"\n转入金额"+(Double.parseDouble(transAmt) / 100.0)+
//                            "\n交易状态："+Status+
//                            "\n终端编号："+transPriorSettle.getTerminalId();
//                    JdPush.MSG_CONTENT="交易信息提醒！";
//                    JdPush.TITLE="花生米移动支付";
//                    JdPush.REGISTRATION_ID=u.getRegistration_id();
//                    JdPush.testSendPush(appKey, masterSecret);
//                }

            } else {
                log.warn("转账时, 用户Id=" + userId + " 对应的用户不存在!");
                sendAppErrorMsg("6012", userId + "");
            }
        } catch (ConnectException e) {//请求 超时
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            user = null;
            send = null;
            transOrder = null;
            transPriorSettle = null;
            result = null;
        }
        return null;
    }

    /**
     * 检测卡卡转账 是否可用
     *
     * @return
     * @throws Exception
     */
    public void checkTransfer() throws Exception {
        log.debug("--- checkCreditBankCard() start ---");
        roleId = Long.parseLong(getJsonString("roleId"));
        clientType = Integer.parseInt(getJsonString("clientType"));
        terminalTypeId = Long.parseLong(getJsonString("terminalTypeId"));
        userId = Long.parseLong(getJsonString("userId"));
        functionKey = getJsonString("functionKey");
        accountNumber2 = getJsonString("accountNumber2");
        transAmt = getJsonString("transAmt");

        if (null == userId) {
            log.warn("检测卡卡转账时, 传入参数userId为空");
            sendAppErrorMsg("6001", "userId");
            return;
        }
        if (StringUtils.isBlank(functionKey)) {
            log.warn("检测卡卡转账时, 传入参数functionKey为空");
            sendAppErrorMsg("6001", "functionKey");
            return;
        }
        if (StringUtils.isEmpty(accountNumber2)) {
            log.warn("检测卡卡转账时, 传入参数accountNumber2为空");
            sendAppErrorMsg("6001", "accountNumber2");
            return;
        }
        if (StringUtils.isEmpty(transAmt)) {
            log.warn("检测卡卡转账时, 传入参数transAmt为空");
            sendAppErrorMsg("6001", "transAmt");
            return;
        }
        try {
            functionKey = TRANSFER_FUNCKEY;
            if (!checkPIV(functionKey)) {
                log.warn("检测卡卡转账时, 银行接口暂停使用！");
                return;
            }

            //屏蔽伪卡
            if (transRefuseCardService.isRefuseCard(accountNumber2)) {
                sendAppErrorMsg("6073", accountNumber);
                return;
            }

            TerminalPrivileges tp = terminalPrivilegesService.searchPrivilegesByFunctionKey(TRANSFER_FUNCKEY);
            Integer channelCode = tp.getTchannel().intValue();

            if (null == channelCode){
                channelCode = PurchaseBaseAction.TC_ZHONGYI;
            }

            Map<String, Object> result = new HashMap<String, Object>();
            switch (channelCode) {
                case PurchaseBaseAction.TC_ZHONGYI:
                    FeeResult fr = getZYFee(accountNumber2, transAmt,roleId);
                    result.put("reqCode", fr.getReqCode());
                    result.put("reqMsg", fr.getReqMsg());
                    result.put("fee", StringUtils.isEmpty(fr.getFee()) ? "" : fr.getFee());
                    result.put("isNeedNameId", StringUtils.isEmpty(fr.getIsNeedNameID()) ? "" : fr.getIsNeedNameID());
                    break;
            }
            sendAppMsg(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public FeeResult getZYFee(String accountNumber2, String transAmt,long roleId) throws Exception {
        FeeResult fr = ds_zytlPayService.checkTransfer(accountNumber2, transAmt,roleId);
        return fr;
    }
}
